﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace OpenShape
{
    public partial class LayerColor : Form
    {
        public event Deligate_LayerColorChanged LayerColor_LayerColorChanged; // 델리게이트
        Color lc_penColor = new Color();
        Color lc_brushColor = new Color();
        float lc_penThickness;
        int lc_ShapType;
        int isID;

        public LayerColor(Color penColor, Color brushColor, float penThickness, int ShapeType, int id)
        {
            InitializeComponent();

            lc_brushColor = brushColor;
            lc_penColor = penColor;
            lc_penThickness = penThickness;
            lc_ShapType = ShapeType;
            isID = id;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.lc_penColor = dlg.Color;
                pbPreView.Invalidate();
                pbLineColor.Invalidate();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.lc_brushColor = dlg.Color;
                pbPreView.Invalidate();
                pbBrushColor.Invalidate();
            }
        }

        private void LayerColor_Load(object sender, EventArgs e)
        {
            pbPreView.Invalidate();
            pbLineColor.Invalidate();
            pbBrushColor.Invalidate();
            updownThickness.Value = (decimal)lc_penThickness;

            if (lc_ShapType == 3)
            {
                pbBrushColor.Visible = false;
                button1.Visible = false;
            }
        }

        private void pbPreView_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pPen = new Pen(lc_penColor, lc_penThickness);
            SolidBrush pBrush = new SolidBrush(lc_brushColor);
            g.Clear(SystemColors.ControlLightLight);

            switch (lc_ShapType)
            {
                case 1:

                    g.FillEllipse(pBrush, pbPreView.Width / 2 - 2, pbPreView.Height / 2 - 2, 4, 4);
                    g.DrawEllipse(pPen, pbPreView.Width / 2 - 2, pbPreView.Height / 2 - 2, 4, 4);

                    break;

                case 3:
                    g.DrawLine(pPen, 0, pbPreView.Height / 2, pbPreView.Width, pbPreView.Height / 2);
                    break;

                case 5:
                    g.FillRectangle(pBrush, 4, 4, pbPreView.Width - 8, pbPreView.Height - 8);
                    g.DrawRectangle(pPen, 4, 4, pbPreView.Width - 8, pbPreView.Height - 8);
                    break;
            }
        }

        private void pbLineColor_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush pBrush = new SolidBrush(lc_penColor);
            g.Clear(SystemColors.ControlLightLight);

            switch (lc_ShapType)
            {
                case 1:
                    g.FillRectangle(pBrush, 1, 1, pbLineColor.Width - 2, pbLineColor.Height - 2);
                    break;

                case 3:
                    g.FillRectangle(pBrush, 1, 1, pbLineColor.Width - 2, pbLineColor.Height - 2);
                    break;

                case 5:
                    g.FillRectangle(pBrush, 1, 1, pbLineColor.Width - 2, pbLineColor.Height - 2);
                    break;
            }
        }

        private void pbBrushColor_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen pPen = new Pen(lc_penColor, lc_penThickness);
            SolidBrush pBrush = new SolidBrush(lc_brushColor);
            g.Clear(SystemColors.ControlLightLight);

            switch (lc_ShapType)
            {
                case 1:
                    g.FillRectangle(pBrush, 1, 1, pbBrushColor.Width - 2, pbBrushColor.Height - 2);
                    break;

                case 3:

                    break;

                case 5:
                    g.FillRectangle(pBrush, 1, 1, pbBrushColor.Width - 2, pbBrushColor.Height - 2);
                    break;
            }
        }

        private void updownThickness_ValueChanged(object sender, EventArgs e)
        {
            lc_penThickness = (float)updownThickness.Value;
            pbPreView.Invalidate();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LayerColor_LayerColorChanged(lc_penColor, lc_brushColor, lc_penThickness, isID);
            this.Dispose();
        }
        
    }
}